package server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * Created by 10188 on 2018/3/6.
 */
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline=socketChannel.pipeline();

        // 以("\n")为结尾分割的解码器，最大帧长度为8192
        // Delimiter Based Frame Decoder 基于定界符的帧解码器
        // Delimiters.lineDelimiter()行分隔符
        // 这里使用时，会把换行符作为一个消息的分隔界限使用，即接收时，换行符前为一条消息
        pipeline.addLast("framer",new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));

        // 字符串解码和编码
        // encoder 编码器， decoder 解码器
        pipeline.addLast("decoder",new StringDecoder());
        pipeline.addLast("encoder",new StringEncoder());

        // 自己的逻辑Handler
        pipeline.addLast("handler",new NettyServerHandler());
    }
}
